const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    check_box_text:'全选',
    isDis:false,//是否批量分配
    poolOption:[],
    date_popup_show:false,
    root:app.domain,
    page:1,//页数
    list:[],//客户数组
    keyword:'',//搜索关键词
    belongOption:[],
    belongTag:'',//客户归属
    labelTag:'',
    labelOption:[],
    sort_show:false,
    sortOption: [],
    sort:'',
    filter_show:false,
    sourceOption:[],
    sourceTag:'',
    stateOption:[],
    stateTag:'',
  },

   
  onSwipeClose(event){
    let that = this;
    const { position, instance } = event.detail;
    switch (position) {
      case 'right':
        wx.showModal({
          title: '提示',
          content: '确定删除吗？',
          success (res) {
            if (res.confirm) {
              api.curlPost('?cla=clientOtherApp&fun=delPublicClient',{khid:event.currentTarget.dataset.id}).then((res)=>{
                that.deleteData(event.currentTarget.dataset.id)
                wx.showToast({
                  title: res.warn,
                })
              })
            } 
          },
          complete(){
            instance.close();
          }
        })
        break;
    }
  },

  /**删除数据 */
  deleteData(id){
    let list = this.data.list;
    let newList = list.filter(item => item.khid !=id );
    this.setData({
      list:newList
    })
  },

  onCheckBoxChange(event) {
    let arr = [];
   
    this.data.list.forEach((v)=>{
      if(v.khid == event.currentTarget.dataset.id){
        v.checked = event.detail;
      }
      arr.push(v)
    })
   
    this.setData({
      list: arr,
    });
  },
  chooseAll(){
    if (this.data.check_box_text == '全选') {
      let arr = [];
      this.data.list.forEach((v)=>{
        v.checked = true;
        arr.push(v)
      })
      this.setData({
        check_box_text:'取消全选',
        list:arr
      })
    }else{
      let arr = [];
      this.data.list.forEach((v)=>{
        v.checked = false;
        arr.push(v)
      })
      this.setData({
        check_box_text:'全选',
        list:arr
      })
    }
  },
 

  showCheckBox(){
    this.setData({
      isDis:!this.data.isDis,
      
    })
    
  },
  distribute(){
    this.showCheckBox();
    this.setData({
      check_box_text:'全选',
    })
    wx.navigateTo({
      url: '../chooseStaff/chooseStaff?type=distribute',
    })
  },
 
  /**获取 */
  obtain(e){
    let that = this;
    api.curlPost('?cla=clientOtherApp&fun=obtain',{khid:e.currentTarget.dataset.id}).then((res)=>{
       that.requestData();
    })
  },
  edit(){
    wx.navigateTo({
      url: '../publicClientEdit/publicClientEdit',
    })
  },
  condition(){
    this.setData({
      filter_show:true,
    })
  },
  /**搜索文字变动 */
  searchChange(e){
    this.setData({
      keyword: e.detail,
    });
  },
  onSearch(){
    this.requestData();
  },
  /**点击待考试试题 */
  detail(e){
    wx.navigateTo({
      url: '../publicClientInfo/publicClientInfo?id='+e.currentTarget.dataset.id,
    })
  },
  /**底部tabar切换 */
  onChange(event){
    let that = this;
    let index = event.detail;
    that.setData({
      active: index,
    })
    this.requestData()
  },

  /**筛选 */
  chooseTag(e){
    let value = e.currentTarget.dataset.value;
    let type = e.currentTarget.dataset.type;
    switch(type){
      case 'workFlow':
        this.setData({
          stateTag:this.data.stateTag==value?'':value
        })
        break;
      case 'belong':
        this.setData({
          belongTag:this.data.belongTag==value?'':value
        })
      break;
      case 'source':
        this.setData({
          sourceTag:this.data.sourceTag==value?'':value
        })
      break;
      case 'label':
        this.setData({
          labelTag:this.data.labelTag==value?'':value
        })
      break;
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    

    api.curlPost('?cla=clientApp&fun=search').then((res)=>{
      that.setData({
        sortOption:res.data.orderBy,
       
        labelOption:res.data.tag,
        sourceOption:res.data.source,
        stateOption:res.data.workFlow
      })
    })
    api.curlPost('?cla=clientApp&fun=params',{type:'pool'}).then((res)=>{
      let option = [];
      res.data.forEach((v)=>{
        option.push(v.text)
      })
      that.setData({
        belongOption:option,
        poolOption:res.data,
      })
    })
  },

  /**
   * 展开排序
   */
  showSortPopup(){
    this.setData({
      sort_show:true,
    })
  },
  /**关闭排序 */
  onSortClose(){
    this.setData({
      sort_show:false
    })
  },
  onCellClick(e){
    this.setData({
      sort:e.currentTarget.dataset.name,
      sort_show:false
    })
    this.requestData();
  },
  onRadioChange(event) {
    this.setData({
      sort: event.detail,
      sort_show:false
    });
    this.requestData();
  },
  cancelFilter(){
    this.setData({
      filter_show:false
    })
  },
  resetTage(){
    this.setData({
      labelTag:'',
      stateTag:'',
      sourceTag:'',
      belongTag:'',
    })
  },
  confirmFilter(){
    this.setData({
      startDate:'',
      endDate:'',
    })
    this.requestData();
    this.cancelFilter();
  },
  /**
   * 请求数据
   */
  requestData:function(page=1){
    let that = this;
    let url = '?cla=clientOtherApp&fun=list&page='+page;
    let poolId = '';
    this.data.poolOption.forEach((v)=>{
      if(v.text == that.data.belongTag){
        poolId = v.value;
      }
    })
      let para = {
        wd:this.data.keyword,
        poolId:poolId,
        tag:this.data.labelTag,
        workFlow:this.data.stateTag,
        source:this.data.sourceTag,
        orderBy:this.data.sort
      }
      api.curlPost(url,para).then((res)=>{
        if(!api.isBlank(res.data.result) || page ==1){
          let arr = page==1?res.data.result:that.data.list.concat(res.data.result);
          that.setData({
            list:arr,
            page:Number(res.data.page.pageNow) +1
          })
        }
        wx.stopPullDownRefresh()
        if (page == 1) {
          that.setData({
            sort:that.data.sort?this.data.sort:res.data.orderBy
          })
        }
      })
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.requestData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestData(this.data.page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})